﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SQLIOSimParser
{
    public class SQLIOSimParserSettings
    {

        private string exportType;
        private string exportDelimiter;
        private bool shareStatistics;

        #region Properties

        public string ExportType
        {
            get { return exportType; }
            set { exportType = value; }
        }

        public string ExportDelimiter
        {
            get { return exportDelimiter; }
            set { exportDelimiter = value; }
        }

        public bool ShareStatistics
        {
            get { return shareStatistics; }
            set { shareStatistics = value; }
        }

        #endregion Properties

        public SQLIOSimParserSettings()
        {
            exportType = GetExportType(Constants.CONST_DEFAULT_EXPORTTYPE);
            exportDelimiter = GetExportDelimiter(Constants.CONST_DEFAULT_EXPORTDELIMITER);
            shareStatistics = GetShareStatistics(Constants.CONST_DEFAULT_SHARESTATISTICS);
        }


        public void Save()
        {
            ValidateSettings();
            SetExportDelimiter();
            SetExportType();
            SetShareStatistics();
        }

        public Dictionary<string, string> ValidateSettings()
        {
            Dictionary<string, string> Errors = new Dictionary<string, string>();
            string Error = string.Empty;

            if(string.IsNullOrEmpty(this.ExportDelimiter))
                Errors.Add("ExportDelimiter", "Export delimiter cannot be empty.");

            return Errors;
        }

        #region Retrieve values

        private bool GetShareStatistics(bool DefaultValue)
        {
            return Convert.ToBoolean(CommonHelper.RegistryHelper.ReadRegistrySeetingsCurrentUser("Software\\SQLIOSimParser", "ShareStatistics", DefaultValue));
        }

        private string GetExportDelimiter(string DefaultValue)
        {
            return CommonHelper.RegistryHelper.ReadRegistrySeetingsCurrentUser("Software\\SQLIOSimParser", "ExportDelimiter", DefaultValue).ToString();
        }

        private string GetExportType(string DefaultValue)
        {
            return CommonHelper.RegistryHelper.ReadRegistrySeetingsCurrentUser("Software\\SQLIOSimParser", "ExportType", DefaultValue).ToString();
        }
        #endregion Retrieve values


        #region Save values
        private void SetShareStatistics()
        {
            CommonHelper.RegistryHelper.SetRegistrySeetingsCurrentUser("Software\\SQLIOSimParser", "ShareStatistics", shareStatistics.ToString());
        }

        private void SetExportDelimiter()
        {
            CommonHelper.RegistryHelper.SetRegistrySeetingsCurrentUser("Software\\SQLIOSimParser", "ExportDelimiter", exportDelimiter);
        }

        private void SetExportType()
        {
            CommonHelper.RegistryHelper.SetRegistrySeetingsCurrentUser("Software\\SQLIOSimParser", "ExportType", exportType );
        }
        #endregion Save values

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0}:{1}|", "ExportDelimiter", exportDelimiter);
            sb.AppendFormat("{0}:{1}|", "ExportType", exportType);

            return sb.ToString();
        }
    }

}